<#include "custom.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>   
package ${basepackage}.${subpackage}.web.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidian.ep.model.CompanyTicket;
import com.yidian.ep.model.PageList;
import com.yidian.ep.model.${className};
import com.yidian.ep.service.${className}Service;
import com.yidian.ep.util.ConfigHelper;
import com.yidian.ep.util.ReturnMsg;
import com.yidian.ep.util.WebConstants;

@Controller
@RequestMapping(value = "${classNameLower}")
public class ${className}Controller {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	@Resource
	${className}Service ${classNameLower}Service;
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public void list(String keyword, Integer currPage, Integer pageSize, Integer status, HttpServletRequest request, HttpServletResponse response) {
		String backMsg = "";
		status = (status == null) ? 0 : status;
		currPage = (currPage == null) ? 1 : currPage;
		keyword = (keyword == null) ? "" : keyword;
		pageSize = (pageSize == null) ? WebConstants.PAGE_SIZE : pageSize;
		PageList pageList = new PageList();
		pageList.setCurrentPage(currPage);
		pageList.setPageSize(pageSize);
		try {
			CompanyTicket ticket = new CompanyTicket(response, request);
			String companyId = ticket.getCompanyID();
			companyId = (companyId == null) ? ConfigHelper.getInstance().getCompany_default_id() : companyId;
			${className} ${classNameLower} = new ${className}();
			${classNameLower}.setStatus(status);
			${classNameLower}.setCompanyId(companyId);
			pageList = ${classNameLower}Service.get${className}List(${classNameLower}, keyword, pageList);
			if (pageList != null) {
				List<Map<String, Object>> list = pageList.getDataSource();
				if (list != null) {
					backMsg = ReturnMsg.plusMsg(list, 0, "", pageList);
				}else{
					backMsg = ReturnMsg.plusMsg("", 2, "暂时没有数据");
				}
			}else{
				backMsg = ReturnMsg.plusMsg("", 2, "暂时没有数据");
			}
		} catch (Exception e) {
			logger.error("---系统异常:[" + e.getMessage() + "]");
			backMsg = ReturnMsg.plusMsg("", 3, "系统异常");
		}
		ReturnMsg.sendMsg(backMsg, response, request);
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void addMsg(${className} ${classNameLower}, HttpServletRequest request, HttpServletResponse response) {
		String backMsg = "";
		try {
			CompanyTicket ticket = new CompanyTicket(response, request);
			${classNameLower}.setCompanyId(ticket.getCompanyID());
			${classNameLower}.setUserId(ticket.getUserID());
			${classNameLower}Service.add${className}(${classNameLower});
			backMsg = ReturnMsg.plusMsg("", 0, "发送成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("---系统异常:[" + e.getMessage() + "]");
			backMsg = ReturnMsg.plusMsg("", 3, "系统异常");
		}
		ReturnMsg.sendMsg(backMsg, response, request);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public void editMsg(${className} ${classNameLower}, HttpServletRequest request, HttpServletResponse response) {
		String backMsg = "";
		try {
			if (${classNameLower}.getId() == 0) {
				backMsg = ReturnMsg.plusMsg("", 1, "id不能为空");
			} else {
				CompanyTicket ticket = new CompanyTicket(response, request);
				${classNameLower}.setCompanyId(ticket.getCompanyID());
				${classNameLower}.setUserId(ticket.getUserID());
				${classNameLower}Service.update${className}(${classNameLower});
				backMsg = ReturnMsg.plusMsg("", 0, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("---系统异常:[" + e.getMessage() + "]");
			backMsg = ReturnMsg.plusMsg("", 3, "系统异常");
		}
		ReturnMsg.sendMsg(backMsg, response, request);
	}

	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public void getProduciton(Integer id, HttpServletRequest request, HttpServletResponse response) {
		String backMsg = "";
		try {
			CompanyTicket ticket = new CompanyTicket(response, request);
			if (id == null) {
				backMsg = ReturnMsg.plusMsg("", 1, "id不能为空");
			} else {
				${className} p = new ${className}(id);
				${className} ${classNameLower} = ${classNameLower}Service.get${className}ById(p);
				if (${classNameLower} != null) {
					backMsg = ReturnMsg.plusMsg(${classNameLower}, 0, "");
				} else {
					backMsg = ReturnMsg.plusMsg("", 2, "没有数据");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("---系统异常:[" + e.getMessage() + "]");
			backMsg = ReturnMsg.plusMsg("", 3, "系统异常");
		}
		ReturnMsg.sendMsg(backMsg, response, request);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public void ${classNameLower}Delete(String[] ids, HttpServletRequest request, HttpServletResponse response) {
		String backMsg = "";
		try {
			CompanyTicket ticket = new CompanyTicket(response, request);
			if (ids != null) {
				${classNameLower}Service.delete${className}(ids, ticket.getCompanyID());
				backMsg = ReturnMsg.plusMsg("", 0, "");
			} else {
				backMsg = ReturnMsg.plusMsg("", 1, "参数不能为空");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("---系统异常:[" + e.getMessage() + "]");
			backMsg = ReturnMsg.plusMsg("", 3, "系统异常");
		}
		ReturnMsg.sendMsg(backMsg, response, request);
	}

}
